prepare("SELECT id FROM subjects WHERE code = ?"); $stmt->execute([$code]); if ($stmt->rowCount() > 0) { $error = "Subject code already exists! Please use a different code."; } else { $stmt = $pdo->prepare("INSERT INTO subjects (name, code, description) VALUES (?, ?, ?)"); $stmt->execute([$name, $code, $description]); $success = "Subject created successfully!"; } } catch (PDOException $e) { $error = "Error creating subject: " . $e->getMessage(); } } // Handle subject update if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_subject'])) { $subject_id = sanitize_input($_POST['subject_id']); $name = sanitize_input($_POST['name']); $code = sanitize_input($_POST['code']); $description = sanitize_input($_POST['description']); try { // Check if subject code already exists (excluding current subject) $stmt = $pdo->prepare("SELECT id FROM subjects WHERE code = ? AND id != ?"); $stmt->execute([$code, $subject_id]); if ($stmt->rowCount() > 0) { $error = "Subject code already exists! Please use a different code."; } else { $stmt = $pdo->prepare("UPDATE subjects SET name = ?, code = ?, description = ? WHERE id = ?"); $stmt->execute([$name, $code, $description, $subject_id]); $success = "Subject updated successfully!"; } } catch (PDOException $e) { $error = "Error updating subject: " . $e->getMessage(); } } // Handle subject deletion if (isset($_GET['delete_id'])) { $delete_id = sanitize_input($_GET['delete_id']); try { // Check if subject is used in any exams (using the new exam_subjects table) $stmt = $pdo->prepare("SELECT COUNT(*) as exam_count FROM exam_subjects WHERE subject_id = ?"); $stmt->execute([$delete_id]); $result = $stmt->fetch(PDO::FETCH_ASSOC); if ($result['exam_count'] > 0) { $error = "Cannot delete subject that is assigned to exams. Please remove the subject from exams first."; } else { // Check if subject has questions $stmt = $pdo->prepare("SELECT COUNT(*) as question_count FROM questions WHERE subject_id = ?"); $stmt->execute([$delete_id]); $question_result = $stmt->fetch(PDO::FETCH_ASSOC); if ($question_result['question_count'] > 0) { $error = "Cannot delete subject that has questions. Please delete or reassign the questions first."; } else { $stmt = $pdo->prepare("DELETE FROM subjects WHERE id = ?"); $stmt->execute([$delete_id]); $success = "Subject deleted successfully!"; } } } catch (PDOException $e) { $error = "Error deleting subject: " . $e->getMessage(); } } // Get all subjects with exam and question counts (updated for new structure) $stmt = $pdo->prepare(" SELECT s.*, COUNT(DISTINCT es.exam_id) as exam_count, COUNT(DISTINCT q.id) as question_count FROM subjects s LEFT JOIN exam_subjects es ON s.id = es.subject_id LEFT JOIN questions q ON s.id = q.subject_id GROUP BY s.id ORDER BY s.name "); $stmt->execute(); $subjects = $stmt->fetchAll(PDO::FETCH_ASSOC); // Get subject for editing $edit_subject = null; if (isset($_GET['edit_id'])) { $edit_id = sanitize_input($_GET['edit_id']); $stmt = $pdo->prepare("SELECT * FROM subjects WHERE id = ?"); $stmt->execute([$edit_id]); $edit_subject = $stmt->fetch(PDO::FETCH_ASSOC); } ?> Manage Subjects - Admin

Manage Subjects

Create and manage subjects for examination organization

Unique code for the subject (max 10 characters)
❌ Cancel

All Subjects ()

0): ?>
Subject Code Description Statistics Actions
No description'; ?>
📊 exam question
✏️ Edit
📖

No Subjects Created Yet

Create your first subject to organize your exams.